package com.movie.modules.jf.service.impl;

import com.movie.common.utils.NumberUtils;
import com.movie.modules.jf.dao.ImageResourceDao;
import com.movie.modules.jf.entity.ImageResourceEntity;
import com.movie.modules.jf.service.ImageResourceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.common.utils.PageUtils;
import com.movie.common.utils.Query;

import com.movie.modules.jf.dao.ActorDao;
import com.movie.modules.jf.entity.ActorEntity;
import com.movie.modules.jf.service.ActorService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author JF-PJJ
 */
@Service("actorService")
@Transactional
public class ActorServiceImpl extends ServiceImpl<ActorDao, ActorEntity> implements ActorService {

    @Autowired
    private ActorDao actorDao;

    @Autowired
    private ImageResourceService imageResourceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String actorName=params.get("actorName")==null?null:params.get("actorName").toString();
        Integer status= NumberUtils.objToInteger(params.get("status"));
        IPage<ActorEntity> page = this.page(
                new Query<ActorEntity>().getPage(params),
                new QueryWrapper<ActorEntity>().like(StringUtils.isNotEmpty(actorName),"actor_name",actorName).eq(status!=null,"status",status).orderByDesc("actor_id")
        );

        return new PageUtils(page);
    }

    @Override
    public List<ActorEntity> getByMovieId(Integer id) {
        return actorDao.getByMovieId(id);
    }

    @Override
    public Map<String, List<ActorEntity>> getSelect() {
        Map<String,List<ActorEntity>> map=new HashMap<>();
        map.put("list",this.list(new QueryWrapper<ActorEntity>().eq("status",1)));
        return map;
    }

    @Override
    public void update(ActorEntity actor) {
        if(actor.getActorId()!=null){
            imageResourceService.remove(new QueryWrapper<ImageResourceEntity>().eq("resource_id",actor.getActorId()).eq("resource_type",2));
        }
        if(actor.getImages()!=null&&actor.getImages().length>0){
            actor.setCover(actor.getImages()[0]);
        }else{
            actor.setCover("cover.jpg");
        }
        if(this.saveOrUpdate(actor)){
            if(actor.getImages()!=null&&actor.getImages().length>0){
                /*处理图片列表*/
                List<ImageResourceEntity> list=new ArrayList<>();
                for(String image:actor.getImages()){
                    ImageResourceEntity imageResource=new ImageResourceEntity();
                    imageResource.setResourceType(2);
                    imageResource.setImageSrc(image);
                    imageResource.setResourceId(actor.getActorId());
                    list.add(imageResource);
                }
                imageResourceService.saveBatch(list);
            }
        }

    }

    @Override
    public Map<String, Object> getInfoById(Integer actorId) {
        List<ImageResourceEntity> imageList=imageResourceService.list(new QueryWrapper<ImageResourceEntity>().eq("resource_id",actorId).eq("resource_type",2));
        Map<String,Object> map=new HashMap<>();
        map.put("actor",this.getById(actorId));
        map.put("imageList",imageList);
        return map;
    }

    @Override
    public void delete(Integer actorId) {
        if(this.removeById(actorId)){
            imageResourceService.remove(new QueryWrapper<ImageResourceEntity>().eq("resource_id",actorId).eq("resource_type",2));
        }
    }

    @Override
    public List<ActorEntity> getActorVip(Integer actorId, Integer vipId) {
        return actorDao.getActorByVipAndActor(actorId, vipId);
    }

    @Override
    public PageUtils getActorVipList(Map<String, Object> params, Integer vipId) {
        IPage<ActorEntity> page=actorDao.getActorByVipAndActorList(new Query<ActorEntity>().getPage(params),vipId);
        return new PageUtils(page);
    }

}